﻿Imports MapleLib.PacketLib

Public Class MaplePacketCreator
    Public Shared Function GetServerList(ByVal World As MapleWorld) As Byte()
        Dim writer As New PacketWriter
        writer.WriteShort(SendHeaders.ServerList)
        writer.WriteByte(World.id) '00 
        writer.WriteMapleString(World.Name) '06 00 53 63 61 6E 69 61
        writer.WriteByte(0) 'writer.WriteByte(world.Flag) 00 
        writer.WriteMapleString(World.EventMessage) '00 00
        writer.WriteByte(&H64)
        writer.WriteByte(0)
        writer.WriteByte(&H64)
        writer.WriteByte(0)
        writer.WriteByte(0)
        Dim lastchannel As Byte = World.Channels.Count
        writer.WriteByte(lastchannel)
        For Each channel As MapleChannel In World.Channels
            writer.WriteMapleString(String.Format("{0}-{1}", World.Name, channel.id))
            writer.WriteInt(channel.load)
            writer.WriteByte(World.id)
            writer.WriteShort(channel.id - 1)
        Next
        writer.WriteInt(0)
        writer.WriteShort(0)
        Return writer.ToArray
    End Function

    Public Shared Function LoggedIn(ByVal c As MapleClient) As Byte()
        Dim writer As New PacketWriter
        writer.WriteShort(SendHeaders.LoggedIn)
        writer.WriteByte(0)
        writer.WriteInt(0)
        writer.WriteByte(0)
        writer.WriteBytes(New Byte() {&H4, 0, 0, &H95})
        writer.WriteMapleString(c.AccountName)
        writer.WriteHexString("03 00 00 00 00 00 00 00 00 00 00 6E E5 D6 A3 2C C7 01 16 00 00 00 8C 42 CB 64 6E 80 AF 22 01")
        Return writer.ToArray
    End Function

    Public Shared Function SendCharList(ByVal c As MapleClient) As Byte()
        Dim writer As New PacketWriter
        writer.WriteShort(SendHeaders.CharList)
        writer.WriteByte(0)
        Dim chars As List(Of MapleCharacter) = c.LoadCharacters
        writer.WriteByte(chars.Count)
        For Each character As MapleCharacter In chars
            AddCharStats(writer, c, character)
            writer.WriteHexString("00 95 2D ED 77 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 A8 66 D7 02 66 29 25 4E F8 FB 9E 0D 78 5E 57 CC 01 B0 FF D8 8E")
            addCharLook(writer, character, False)
        Next
        writer.WriteBool(True) 'pic
        writer.WriteLong(7)
        Return(writer.ToArray)
    End Function

    Public Shared Sub AddCharStats(ByVal writer As PacketWriter, ByVal c As MapleClient, ByVal chr As MapleCharacter)
        With writer
            .WriteInt(chr.Id)
            Dim name As String = chr.Name
            While name.Length < 13
                name = name & ControlChars.NullChar
            End While
            .WriteString(name)
            .WriteByte(chr.Gender)
            .WriteByte(chr.Skin)
            .WriteInt(chr.Face)
            .WriteInt(chr.Hair)

            'Pets
            .WriteLong(0)
            .WriteLong(0)
            .WriteLong(0)

            .WriteByte(chr.Level)
            .WriteShort(chr.Job)
            .WriteShort(chr.Str)
            .WriteShort(chr.Dex)
            .WriteShort(chr.Int)
            .WriteShort(chr.Luk)
            .WriteInt(chr.curHP)
            .WriteInt(chr.maxHP)
            .WriteInt(chr.curMP)
            .WriteInt(chr.maxMP)
            .WriteShort(chr.ap)
            If chr.isExtendSPJob() Then
                Dim sptable As New ExtendedSPTable(chr.Job, chr.sp)
                sptable.addSPData(writer)
            Else
                .WriteShort(chr.sp)
            End If
            .WriteInt(chr.exp)
            .WriteShort(chr.Fame)
            .WriteInt(0)
            .WriteShort(0)
            .WriteInt(chr.MapId)
            .WriteByte(chr.SpawnPoint)
            .WriteInt(&HC2)
            If chr.isExtendSPJob() Then
                .WriteShort(chr.sp) 'TODO: Add total calculation
            Else
                .WriteShort(chr.sp)
            End If
        End With
    End Sub

    Private Shared Sub addCharLook(ByVal writer As PacketWriter, ByVal chr As MapleCharacter, ByVal mega As Boolean)
        writer.WriteByte(chr.Gender)
        writer.WriteByte(chr.Skin)
        writer.WriteInt(chr.Face)
        writer.WriteInt(chr.Job)
        writer.WriteBool(mega)
        writer.WriteInt(chr.Hair)
        addCharEquips(writer, chr)
    End Sub

    Private Shared Sub addCharEquips(ByVal writer As PacketWriter, ByVal chr As MapleCharacter)
        'TODO: MaskedEquip = Cash????
        'TODO: add inventory in the database
        'chr.Inventory.SplitItems() '01 75 4B 0F 00 05 F9 DE 0F 00 06 06 2D 10 00 07 46 5C 10 00 0B FC 00 16 00 0C 48 FA 10 00 0D 6C F9 10 00 0F 58 FB 10 00 10 D4 F9 10 00 11 0B 1F 11 00 31 5E 6D 11 00 32 E2 45 11 00 37 20 E2 11 00 
        'If Not IsNothing(chr.Inventory) Then
        '    For Each item As MapleInventory.Items In chr.Inventory.Equiped
        '        'If item.position <> -11 Then
        '        writer.WriteByte(item.position * -1)
        '        writer.WriteInt(item.id)
        '        'End If
        '    Next
        'End If
        writer.WriteHexString("05 82 DE 0F 00 06 A2 2C 10 00 07 81 5B 10 00 0B 25 53 14 00 37 20 E2 11 00")
        writer.WriteByte(&HFF) 'FF  

        'maskedEquips here?

        writer.WriteByte(&HFF) 'FF 
        writer.WriteByte(0)
        writer.WriteShort(0)
        writer.WriteInt(0) '00 00 00 00
        'If Not IsNothing(chr.Inventory) Then
        '    For Each item As MapleInventory.Items In chr.Inventory.Equiped
        '        If item.position = -11 Then
        '            writer.WriteInt(item.id)
        '        End If
        '    Next
        'End If
        'petequips
        writer.WriteLong(0) '00 00 00 00 00 00 00 00
        writer.WriteInt(0) '00 00 00 00
    End Sub
End Class